<template>
  <div class="roles">
    <!-- 面包屑 -->
    <miAn-xie :betGao="betGao"></miAn-xie>
    <el-card>
      <!-- 添加 -->
      <el-button type="primary" @click="dialogFormVisible = true"
        >添加角色</el-button
      >

      <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
        <el-form :model="form" ref="form" :rules="rules">
          <el-form-item label="角色名称" prop="roleName" :label-width="formLabelWidth">
            <el-input v-model="form.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" :label-width="formLabelWidth">
            <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="roles_jueSe('form')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- 编辑 -->
      <el-dialog title="编辑角色" :visible.sync="dialogForm">
        <el-form :model="Form" ref="Form">
          <el-form-item label="角色名称" prop="roleName" :label-width="formLabelWidth">
            <el-input v-model="Form.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" :label-width="formLabelWidth">
            <el-input v-model="Form.roleDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogForm = false">取 消</el-button>
          <el-button type="primary" @click="roles_cg"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- 表格 -->
      <el-table border :data="roles_list">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div
              class="box"
              :class="{ border_none: i != scope.row.children.length - 1 }"
              v-for="(v, i) in scope.row.children"
              :key="v.id"
            >
              <div class="box_left" >
                <el-tag type="primary" @close="roles_delete(scope.row.id,v.id)" closable effect="dark">
                  {{ v.authName }}
                </el-tag>
              </div>
              <div class="box_right">
                <div
                  class="box_box1"
                  v-for="(vv, ii) in v.children"
                  :key="vv.id"
                >
                  <div
                    class="box_bo1"
                    :class="{ border_none: ii == v.children.length - 1 }"
                  >
                    <el-tag type="success" @close="roles_delete(scope.row.id,vv.id)" closable effect="dark">
                      {{ vv.authName }}--{{ ii }}--{{ v.children.length - 1 }}
                    </el-tag>
                  </div>
                  <div
                    class="box_bo2"
                    :class="{ border_none: ii == v.children.length - 1 }"
                  >
                    <div>
                      <el-tag
                        v-for="vvv in vv.children"
                        :key="vvv.id"
                        type="warning"
                        closable
                        effect="dark"
                        @close="roles_delete(scope.row.id,vvv.id)"
                      >
                        {{ vvv.authName }}
                      </el-tag>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="400px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="roles_biAn(scope.row)"> 编辑 </el-button>
            <el-button type="success" icon="el-icon-delete" @click="roles_sc(scope.row.id)"> 删除 </el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              @click="roles_right(scope.row)"
            >
              分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 树形图 -->
    <el-dialog title="分配权限" :visible.sync="xinAhSi">
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        :props="defaultProps"
        :default-expand-all="true"
        :default-checked-keys="xuanze"
        :getHalfCheckedKeys="roles_con"
        ref="tree"
      >
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="xinAhSi = false">取 消</el-button>
        <el-button type="primary" @click="roles_add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { roles_rights } from "@/http/rights/right.js";
import { roles_Roles,roles_roles_delete, roles_Post,roles_roles,roles_roles_sc ,roles_roles_put} from "@/http/rights/roles.js";
export default {
  data() {
    return {
      // 面包屑
      betGao:[
        {
          'path':'/home',
          'title':'首页'
        },
        {
          'path':'/roles',
          'title':'权限管理'
        },
        {
          'path':'/roles',
          'title':'角色列表'
        },
      ],
      roles_con: [],
      roDI: [],
      data: [],
      xuanze: [],
      roles_list: [],
      dialogFormVisible: false,
      form: {
        roleName: "",
        roleDesc: "",
      },
      formLabelWidth: "100px",
      // 树形图
      defaultProps: {
        children: "children",
        label: "authName",
      },
       xinAhSi: false,
      // 添加
      rules: {
        roleName: [{ required: true, message: "请输入角色名称" }],
      },
      // 编辑
      dialogForm:false,
      Form: {
        roleName: "",
        roleDesc: "",
        id:''
      },
    };
  },
  mounted() {
    this.roles_Role();
  },
  methods: {
    async roles_Role() {
      let res = await roles_Roles();
      // console.log(res);
      this.roles_list = res.data;
    },
    // 权限列表
    async roles_right(row) {
      this.xinAhSi = true;
      let res = await roles_rights("tree");
      // console.log(res);
      this.data = res.data;

      this.xuanze = [];
      this.getChen(row, this.xuanze);
      this.roDI = row.id;
    },
    getChen(row, arr) {
      if (!row.children) {
        arr.push(row.id);
      } else {
        row.children.forEach((item) => this.getChen(item, arr));
      }
    },
    // 重新分配权限
    async roles_add() {
      // console.log('roDI',this.roDI);
      // console.log(this.$refs.tree.getHalfCheckedKeys());
      // console.log(this.$refs.tree.getCheckedKeys());

      var rids = [
        ...this.$refs.tree.getHalfCheckedKeys(),
        ...this.$refs.tree.getCheckedKeys(),
      ].join(",");
      // console.log(rids);
      let res = await roles_Post(this.roDI, { rids });
      this.xinAhSi = false;
      // console.log(res);
      this.roles_Role();
    },
    // 添加角色
    async roles_jueSe(formName) {
      this.$refs[formName].validate( async(valid) => {
        if (valid) {
          let res=await roles_roles(this.form)
          // console.log(res);
          this.dialogFormVisible=false
          this.roles_Role();
        } else {
          return false;
        }
      });
    },
    // 删除
    async roles_sc(id){
      // console.log(id);
      let res=await roles_roles_sc(id)
      // console.log(res);
      this.roles_Role();
    },
    // 编辑
    roles_biAn(row){
      this.dialogForm=true
      let {id,roleDesc,roleName}=row
      this.Form.roleName=roleName
      this.Form.roleDesc=roleDesc
      this.Form.id=id
    },
    async roles_cg(){
      let res =await roles_roles_put(this.Form)
      // console.log(res);
      this.dialogForm=false
      this.roles_Role();
    },
    /**
     * 删除标签
     */
    async roles_delete(id,vid){
      // console.log(id,vid);
      let res=await roles_roles_delete(id,vid)
      console.log(res);
      this.roles_Role();
    }
  },
};
</script>
<style lang="scss" scoped>
.el-table {
  margin: 20px 0;
}
.box {
  width: 100%;
  display: flex;
  border: 1px solid black;
  .box_left {
    width: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-right: 1px solid black;
  }
  .box_right {
    flex: 1;
    .box_box1 {
      display: flex;
      border-bottom: 1px solid black;
      .box_bo1 {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 200px;
        border-right: 1px solid black;
      }
      .box_bo2 {
        display: flex;
        flex-wrap: wrap;
        flex: 1;
        // border-bottom: 1px solid black;
      }
    }
  }
}
.el-tag {
  margin: 10px;
}
.border_none {
  border-bottom: none;
}
</style>